package com.cy.controller;

import com.cy.common.pojo.JsonResult;
import com.cy.common.untils.PageUtil;
import com.cy.pojo.SysUser;
import com.cy.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/selectSysUser/{username}")
    public JsonResult selectSysUser(@PathVariable String username){
        return new JsonResult(sysUserService.selectSysUser(username));
    }

    @PutMapping("/userDisable/{id}/{valid}/{modifiedUser}")
    public JsonResult userDisable(@PathVariable Integer id,
                                  @PathVariable Integer valid,
                                  @PathVariable String modifiedUser){
        return new JsonResult(sysUserService.userDisable(id,valid,modifiedUser));
    }

    @GetMapping("/selectById/{id}")
    public JsonResult selectById(@PathVariable Integer id){
        return new JsonResult(sysUserService.selectById(id));
    }

    @PutMapping("/updateUser")
    public JsonResult updateUser(@RequestBody SysUser sysUser){
        return new JsonResult(sysUserService.updateUser(sysUser));
    }

    @PutMapping("/insertUser")
    public JsonResult insertUser(@RequestBody SysUser sysUser){
        return new JsonResult(sysUserService.insertUser(sysUser));
    }

    @GetMapping("/login/{username}/{password}")
    public JsonResult doLogin(@PathVariable String username,
                              @PathVariable String password){
        //将账号和密码封装 token 对象
        UsernamePasswordToken token = //参考官网
                new UsernamePasswordToken(username, password);
//        token.setRememberMe(true);
        //基于 subject 对象将 token 提交给 securityManager
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);//提交给 securityManager
        return new JsonResult("login ok");
    }
}
